package photostock.model;

import java.io.File;
import java.util.Comparator;
import java.util.Vector;

import photostock.control.ItemCache;

public class UserItem extends File {

	private static final long serialVersionUID = 20090501L;
	protected static String oriPath = "";
	
	public static String FS = File.separator;
	public static String testOriPath = "C:\\Documents and Settings\\Administrateur\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\Photostock\\images\\albums";
	
	private int id;
	private String hardPath;
	private String webPath = "/Photostock/images/albums";
	private UserItem parent;
	protected Vector<UserItem> children = null;
	protected ItemInfo infos;
	protected String query;
	protected String neededFields;
	protected String childrenFields;
	protected boolean created = false;
	
	public UserItem(String webName) {
		super((oriPath.equals("") ? testOriPath : oriPath) + FS + webName);	
		setHardPath(webName);
		if (!exists())  {
			create();
			created = true;
		}
		setWebPath(webName);
	}

	public UserItem(UserItem parent, String name) {
		super(parent.getHardPath() + FS + name);	
		setParent(parent);
		setHardPath();
		if (!exists())  {
			create();
			created = true;
		}
		setWebPath();
	}

	public UserItem(String user, String album, String name) {
		super(oriPath + FS + user + FS + album + FS + name);
		UserItem.initParentItem(this);
		setHardPath(user,album);
		setWebPath(user,album);
		
	}

	
	public boolean create() {
		if ((parent instanceof photostock.model.User ||  this instanceof photostock.model.User) && !exists()) return mkdir();
		return false;
	}
	
	public boolean create(File f) {
		File dest = new File(getHardPath() + FS + f.getName());
		return f.renameTo(dest);
	}
	
	public ItemInfo initItem(UserItem item) {
		return new ItemInfo(item.getNeededFields(), ItemFactory.initItem(item.getRecQuery()));
	}

	public boolean init() {
		return true;
	}
	
	public boolean recItem(UserItem item) {
		boolean res = ItemFactory.recItem(item.getRecQuery());
		if (res) {
			item.init();
			if (!item.isDirectory()) {
				ItemFactory.cache(item);
				for (UserItem i : item.getChildrenItems()) {
					i.setInfos(item.getInfos());
					ItemFactory.cache(i);
				}
			}
		}
		return res;
	}

	public boolean updateItem(UserItem item) {
		boolean res = ItemFactory.updateItem(item.getUpdateQuery());
		if (res) {
			item.init();
			if (!item.isDirectory()) {
				ItemFactory.cache(item);
				for (UserItem i : item.getChildrenItems()) {
					i.setInfos(item.getInfos());
					ItemFactory.cache(i);
				}
			}
		}
		return res;
	}

	public boolean deleteItem(UserItem item) {
		for (UserItem ui : item.getChildrenItems()) {
			if (ItemFactory.isCached(ui)) ItemFactory.uncache(ui);
			ui.delete();
		}
		if (ItemFactory.isCached(item)) ItemFactory.uncache(item);
		super.delete();
		return ItemFactory.deleteItem(item.getDeleteQuery());
	}

	public UserItem getParentItem() {
		return parent;
	}
	
	public void setParent(UserItem parent) {
		this.parent = parent;
	}
	
	public void add(UserItem child) {
		if (null != children && !children.contains(child)) {
			children.add(child);
			if (!child.isDirectory()) ItemCache.getInstance().addToCache(child);

		}
	}
	
	public void remove(UserItem child) {
		if (null != children && children.contains(child)) children.remove(child);
	}
	
	public Vector<UserItem> getChildrenItems() {
		return children;
	}
	
	public Vector<ItemInfo> getChildrenInfos() {
		return ItemFactory.getChildrenInfos(this, query);
	}
	
	public static void setOriPath(String s) {
		oriPath = s;
	}

	public static String getOriPath() {
		if (null == oriPath) return testOriPath;
		return oriPath;
	}
	
	private void setHardPath(String s) {
		hardPath = ((null == parent) ? testOriPath : parent.getHardPath()) + FS + s;
	}

	protected void setHardPath(String user, String album) {
		hardPath = (oriPath.equals("") ? testOriPath : oriPath) + FS + user + FS  + album + FS + getName();
	}

	
	private void setHardPath() {
		hardPath = ((null == parent) ? testOriPath : parent.getHardPath()) + FS + getName();
	}
	
	private void setWebPath(String s) {
		webPath = (null == parent ? webPath : parent.getWebPath()) + "/" + s;
	}
	
	protected void setWebPath(String user, String album) {
		webPath = "/Photostock/images/albums/" + user + "/" + album + "/" + getName();
		
	}

	private void setWebPath() {
		webPath = parent.getWebPath() + "/" + getName();
	}

	public String getRecQuery() {
		return query;
	}

	public String getUpdateQuery() {
		return query;
	}
	
	public String getDeleteQuery() {
		return query;
	}

	public String getInitChildrenQuery() {
		return query;
	}

	
	public String getNeededFields() {
		return neededFields;
	}
	
	public String getChildrenFields() {
		return childrenFields;
	}

	public String getHardPath() {
		return hardPath;
	}
	
	public String getWebPath() {
		return webPath;
	}
	
	public void setId(int i) {
		this.id = i;
	}

	public int getId() {
		return this.id;
	}
	
	public boolean created() {
		return created;
	}
	
	public ItemInfo getInfos() {
		return infos;
	}

	public void setInfos(ItemInfo infos) {
		this.infos = infos;
	}
	
	public static void initParentItem(UserItem item) {
		item.setParent(new UserItem(""));
	}
	
	public ItemComparator getIdComparator() {
		return new ItemComparator();
	}
	
	
	class ItemComparator implements Comparator<UserItem> {
		public int compare(UserItem ui1, UserItem ui2) {
			return new Integer(ui1.getId()).compareTo(new Integer(ui2.getId()));
		}
	}
	
	
}
